﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace Remodel
{
    public partial class subpage : Form
    {
        WebBrowser subpageload = new WebBrowser();        
        HtmlElement subtable;        
        bool headerrun = false;
        public static Excel.Application xlpr;
        public static Excel.Workbook wbpr;
        public static Excel.Worksheet wprc = new Excel.Worksheet();
        public static Excel.Worksheet wprp = new Excel.Worksheet();
        HtmlElementCollection row;
        HtmlElementCollection cell;
        HtmlElementCollection rcell;
        DateTime dt;
        int lastrow;
        
        public subpage()
        {
            InitializeComponent();            
        }

        public void subpage_Load(object sender, EventArgs e)
        {
            subpageload.AllowNavigation = true;
            subpageload.Navigate(spock.address, false);
        }

        private void subpageload_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            subtable = subpageload.Document.All["VR_be43rqkut_5"];
            subpageload.Stop();
            subpageload.Dispose();
            //checkopen();
            openxl();
            if (spock.headerrow == false)
            {
                header();
            }
            body();
        }

        private void openxl()
        {
            if (spock.isopen == false)
            {
                xlpr = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
                xlpr.Visible = true;
                xlpr.DisplayAlerts = false;
                wbpr = xlpr.Workbooks.Open("\\\\u:\\Reports\\Spock\\Plan Revisions.xlsx", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
                wprc = wbpr.Worksheets.Add(Type.Missing, wbpr.Worksheets[wbpr.Worksheets.Count], Type.Missing, Type.Missing);
                wprp = wbpr.Worksheets[wbpr.Worksheets.Count - 1];
                dt = DateTime.Today;
                string wsn = String.Format("{0:MM.dd.yy}", dt);
                wprc.Name = wsn;
                xlpr.DisplayAlerts = true;
                //xlpr.ScreenUpdating = false;
                //xlpr.Calculation = Excel.XlCalculation.xlCalculationManual;
                spock.isopen = true;
            }
        }

        private void header()
        {
            wprc.Range["A1"].Value = spock.wsc.Cells[1, 1].Value;
            wprc.Range["B1"].Value = spock.wsc.Cells[1, 2].Value;
            wprc.Range["C1"].Value = spock.wsc.Cells[1, 3].Value;
            wprc.Range["D1"].Value = spock.wsc.Cells[1, 4].Value;
            wprc.Range["E1"].Value = spock.wsc.Cells[1, 5].Value;
            wprc.Range["F1"].Value = spock.wsc.Cells[1, 6].Value;
            wprc.Range["G1"].Value = "Plan Typ";
            wprc.Range["H1"].Value = "OTP Prj";
            wprc.Range["I1"].Value = "OTP Act";
            wprc.Range["J1"].Value = "Permit Prj";
            wprc.Range["K1"].Value = "Permit Act";
            wprc.Range["L1"].Value = "Admin";
            wprc.Range["M1"].Value = "Prj Arch";
            wprc.Range["N1"].Value = "QC";
            spock.headerrow = true;
        }

        private void body()
        {
            row = subtable.GetElementsByTagName("tr");
            int rc = row.Count;

            for (int r = 2; r < rc; r++)
            {
                lastrow = wprc.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;
                
                if (r == 2)
                {
                    wprc.Cells[lastrow + 1, 1] = spock.rec.InnerText;
                    wprc.Cells[lastrow + 1, 2] = spock.str.InnerText;
                    wprc.Cells[lastrow + 1, 3] = spock.seq.InnerText;
                    wprc.Cells[lastrow + 1, 4] = spock.city.InnerText;
                    wprc.Cells[lastrow + 1, 5] = spock.st.InnerText;
                    wprc.Cells[lastrow + 1, 6] = spock.strtyp.InnerText;
                }
                rcell = row[r].GetElementsByTagName("td");
                int cc = rcell.Count;
                
                for (int c = 1; c < cc - 2; c++)
                {
                    HtmlElement td = rcell[c];                   
                    wprc.Cells[lastrow + 1, c + 6] = td.InnerText;
                }
            }
            spock.newpage.Close();
            spock.newpage.Dispose();
            subpageload.Dispose();
            subtable = null;
            GC.Collect();
        }
    }
}
